<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\ScoreLog;
use think\exception\DbException;
use think\response\Json;

class UserScore extends Backend
{
    /**
     * Bill模型对象
     * @var \app\common\model\Bill
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ScoreLog();


        $this->assignconfig('statusList', ScoreLog::STATUS_ARRAY);
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        $this->relationSearch = true;
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with(['user', 'staff'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        foreach ($list as $item) {
            $item['score'] = $item['type'] == 1 ? '+' . $item['score'] : '-' . $item['score'];
        }


        $scoreList = $this->model
            ->with(['user', 'staff'])
            ->where($where)
            ->select();
        $score = 0;
        foreach ($scoreList as $value){
            $score += $value['type'] == 1 ? $value['score'] : -$value['score'];
        }


        $result = ['total' => $list->total(), 'rows' => $list->items(),'data'=>['score'=>$score]];
        return json($result);
    }


}